﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.util;
using System.Text.RegularExpressions;
using iTextSharp.text.pdf.draw;
using System.Diagnostics;

namespace Reader.Libs.PDF
{
    public class HtmlToPdf
    {
        public void CreatePDFFromHTMLFile()
        {
            try
            {
                // set the file name
                string file = "MyPdf.pdf";

                // create a pdf document
                Document document = new Document();

                // set the page size, set the orientation
                document.SetPageSize(PageSize.A4);

                // create a writer instance
                PdfWriter pdfWriter = PdfWriter.GetInstance(document, new FileStream(file, FileMode.Create));

                // open the document
                document.Open();

                // html pagina inlezen
                string htmlText = "<html><head><title>HTML template</title></head><body><div><h1>Convert a HTML-file to a PDF-document!</h1></div></body></html>";

                // html pagina parsen in een arraylist van IElements
                List<IElement> htmlElements = HTMLWorker.ParseToList(new StringReader(htmlText), null);

                // add the IElements to the document
                for (int i = 0; i < htmlElements.Count; i++)
                {
                    // cast the element
                    IElement htmlElement = ((IElement)htmlElements[i]);
                    document.Add(htmlElement);
                }

                // close the document
                document.Close();

                // open the pdf document
                Process.Start(file);
            }
            catch (System.Exception)
            {
            	
            }
        }
    }
}
